﻿using System;
using System.Collections.Generic;
using BML.Infrastructure.Implement;
using BML.Infrastructure.Platform;
using Model.Platform.PhysicalData;
using Service.Infrastructure.Interface;
using Service.Infrastructure.Model;
using Service.Infrastructure.Result;
using Service.Work.Model;
using SystmicFunction.Public.CacheService;

namespace Service.Infrastructure.Implement
{
    /// <summary>
    ///  管理员操作
    /// </summary>
    public class ManagerService : BaseService, IAccount, INotifications, ITask, IPlatformInitialization
    {
        private readonly ManagerOp _managerOp = new ManagerOp();
        private readonly SystemicLoggerOp _systemicLogger = new SystemicLoggerOp();

        public const Int32 Administrator = 1;
        public const Int32 Manager = 2;
        /// <summary>
        ///     系统通知与站内信
        /// </summary>
        /// <returns></returns>
        public ServiceResult CountNotifications()
        {
            return Result(ResultType.OperateQueryIsSuccess, _systemicLogger.CountLog());
        }

        public ServiceResult Login(string userKey, params object[] info)
        {
            if (ParamsIsNull(userKey, info[0]))
                return Result(ResultType.ParmamIsNull);
            var account = _managerOp.QueryAccount(userKey, (String)info[0]);
            if (ParamsIsNull(account))
                return Result(ResultType.AccountLoginFaile);

            //新建内存ID，保存内存数据
            var sessionKey = MemoryCache.GenerateKey();
            KeepSession(sessionKey, account);
            return Result(ResultType.AccountLoginSuccess, sessionKey);
        }

        public ServiceResult Reg(string userKey, params object[] info)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///     
        ///      初始需要向info[0]传入数据库中的真实Id 
        /// </summary>
        /// <param name="sessionKey">缓存中的ID，这个ID可以公开(且具有时效性)</param>
        /// <param name="info"></param>
        /// <returns></returns>
        public ServiceResult KeepSession(String sessionKey, params object[] info)
        {
            //缓存中是否存在Key
            //var cacheData = LocalMemoryCache.GetCache(sessionKey);

            //从缓存中获取管理员的数据库Id
            Object cacheData = null;

            if (ParamsIsNull(info[0]))
            {
                //查询相关信息
                cacheData = CoverModel((ManagerAccount)info[0]);//TODO：更新怎么做
            }

            //更新或新设缓存
            LocalMemoryCache.SetCache(sessionKey, cacheData);

            return Result(ResultType.Success, sessionKey);
        }

        private ManagerModel CoverModel(ManagerAccount obj)
        {
            return new ManagerModel { Id = obj.Id, Name = obj.Name, Role = obj.RoleType };
        }

        /// <summary>
        ///     会员管理操作
        /// </summary>
        /// <returns></returns>
        public ServiceResult QueryUsers()
        {
            var users = new List<UsersModel>();

            for (int i = 1; i < 50; i++)
            {
                users.Add(new UsersModel
                {
                    Id = Guid.NewGuid(),
                    Account = "Account" + i,
                    Birthday = DateTime.Now.ToShortDateString() + "B",
                    CreateTime = DateTime.Now.ToShortDateString() + "C",
                    Email = "Email" + i,
                    LoginTime = DateTime.Now.ToShortDateString() + "L",
                    Mobile = "134..." + i,
                    Name = "name" + i,
                    NickName = "nickName" + i,
                    Sex = "男",
                    Type = "普通用户"

                });
            }


            return Result(ResultType.OperateQueryIsSuccess, users);
        }

        public ServiceResult PendingTasks()
        {
            var list = new List<TaskModel>();
            for (Int32 i = 1; i < 6; i++)
            {
                list.Add(new TaskModel
                {
                    Id = Guid.NewGuid().ToString(),
                    Description = "任务描述" + i,
                    Percent = String.Format("{0}0%", i),
                    Type = i
                });
            }
            return Result(ResultType.OperateQueryIsSuccess, list);
        }


        public void Init()
        {
            //TODO:初始化管理员
            _managerOp.CreateAdministrater("admin", "dongyan", "Se7en", Administrator);

        }
    }
}
